package com.aisi.manager.config;

import com.aisi.commonUtil.AuthContextUtil;
import com.aisi.manager.interceptor.LoginAuthInterceptor;
import com.aisi.manager.properties.UserAuthProperties;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: shenjianZ
 * @Date: 2024/2/16 21:11
 * @Description:
 */

@Component
public class WebMVCConfiguration implements WebMvcConfigurer {

    @Resource
    private LoginAuthInterceptor loginAuthInterceptor;

    @Resource
    private UserAuthProperties userAuthProperties;

    /**
     * 添加跨域映射。
     *
     * @param registry CorsRegistry 对象，用于配置跨域请求的规则
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")  // 允许的来源模式
                .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS","UPDATE") // 允许的请求方式
                .allowCredentials(true);
    }


    /**
     * 向拦截器注册表中添加拦截器
     *
     * @param registry 拦截器注册表
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
//                .excludePathPatterns("/admin/system/index/login")
//                .excludePathPatterns("/admin/system/index/generateValidateCode")
                .excludePathPatterns(userAuthProperties.getNoAuthUrls())
                .addPathPatterns("/**");
    }
}
